﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using AMM.Business.Store;
using AMM.CustomWinControls;
using AMM.Model;
using AMM.Model.Store;
using AMM.UserControls;

namespace AMM.Forms.Store {
    public partial class FrmProductInventory : FrmProductInventoryBase {

        private int _pageIndex;
        private ProductInventorySearchModel _searchModel;
        private ProductInventoryProcessor _productProductInventoryProcessor;
        private List<ProductInventoryViewModel> _productProductInventoryList;

        #region Override

        protected override string FunctionCode {
            get { return "AMM1414"; }
        }

        protected override BaseSearchControl SearchControl {
            get { return new ProductInventorySearchControl(); }
        }

        protected override void Add() {
            this.ResetForms();
            this.ShowEditForm("新增", new ProductInventory() { ClassStatus = ClassStatus.Added });
            base.Add();
        }

        protected override void Amend() {
            List<ProductInventoryViewModel> selectedList = this.dgvProductInventoryList.GetSelectedRows<ProductInventoryViewModel>();
            if (selectedList.Count > 1) { this.OnlySelectSingleRecord(); return; }
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            ProductInventory productProductInventory = this._productProductInventoryProcessor.FetchProductInventoryById(selectedList[0].Key);
            productProductInventory.ClassStatus = ClassStatus.Updated;
            this.ShowEditForm("修改", productProductInventory);
            base.Amend();
        }

        protected override void Save() {
            base.Save();
        }

        protected override void Delete() {
            List<ProductInventoryViewModel> selectedList = this.dgvProductInventoryList.GetSelectedRows<ProductInventoryViewModel>();
            if (selectedList.Count == 0) { this.ShouldSelectSingleRecord(); return; }
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._productProductInventoryProcessor.Delete(selectedList.Select(x => x.Key).ToArray());
                this.MDIParent.SetStatusInformation("删除成功");
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                base.Delete();
            });
        }       

        protected override void Search(object sender, FrmSearch.SearchArgs e) {
            this.CreateNewThread(() => {
                if (e.IsRevert) {
                    this._searchModel = new ProductInventorySearchModel();
                }
                else {
                    this._searchModel = (ProductInventorySearchModel)e.SearchModel;
                }
            }, () => {
                this.BindProductInventoryList();
            });
        }

        #endregion

        public FrmProductInventory() {
            InitializeComponent();

            this.dgvProductInventoryList.DataGridViewModelType = typeof(ProductInventoryViewModel);
        }

        #region Private methods

        private bool frmEdit_Edit(object sender, FrmEdit.EditArgs e) {
            bool resultFlag = false;
            ProductInventory productProductInventory = (ProductInventory)e.EditModel;
            this.PreventCancel = true;
            this.CreateNewThread(() => {
                if (productProductInventory.ClassStatus == ClassStatus.Added) {
                    productProductInventory.CreatedBy = LoginUserName;
                    this._productProductInventoryProcessor.Insert(productProductInventory);
                }
                else {
                    productProductInventory.ModifiedBy = LoginUserName;
                    this._productProductInventoryProcessor.Update(productProductInventory);
                }
                resultFlag = true;
            }, () => {
                this.ResetForms();
                this.PreventCancel = false;
                if (resultFlag) {
                    this.MDIParent.SetStatusInformation("保存成功");
                    this.FormEdit.Close();
                }
            });

            this.Save();
            return true;
        }

        private void ResetForms() {
            this.BindProductInventoryList();
        }

        private void BindProductInventoryList() {
            this.CreateNewThread(() => {
                this._productProductInventoryList = this._productProductInventoryProcessor.FetchProductInventoryViewModelList(this._searchModel, this._pageIndex);
            }, () => {
                this.dgvProductInventoryList.DataSource = new SortableBindingList<ProductInventoryViewModel>(this._productProductInventoryList);
                this.pceList.Records = this._searchModel.Records;
                this.pceList.DataBind();
            });
        }

        private void ShowEditForm(string title, ProductInventory productProductInventory) {
            this.CreateNewThread(() => {
                Thread.Sleep(100);
            }, () => {
                ProductInventoryEditControl editControl = new ProductInventoryEditControl();
                this.FormEdit = new FrmEdit();
                editControl.BaseEditModel = productProductInventory;
                editControl.ValidatorForEdit = this.FormEdit.validatorForEdit;
                this.FormEdit.Controls.Add(editControl);
                this.FormEdit.Edit += new FrmEdit.EditEventHandler(frmEdit_Edit);
                this.FormEdit.Text = string.Format("{0}{1}", title, this.Text);
                FrmEntry.LoadAndRunDialog(this.FormEdit);
            });
        }

        #endregion

        private void FrmProductInventory_Load(object sender, EventArgs e) {
            this.CreateNewThread(() => {
                this.PreventCancel = true;
                this._productProductInventoryProcessor = new ProductInventoryProcessor(this.pceList.PageSize);
                this._searchModel = new ProductInventorySearchModel();
                this._pageIndex = 1;
            }, () => {
                this.WindowState = FormWindowState.Maximized;
                this.ResetForms();
                this.PreventCancel = false;
            });
        }

        private void pceList_Paging(object sender, PaginationArgs e) {
            this._pageIndex = e.PageIndex;
            this.BindProductInventoryList();
        }
    }
}
